(function () {
    window.config = {}


}());

/**
 * Created by EEO on 2017/6/5. 分类数据绑定
 */
var indexRender = (function () {
    var $content = $('#content');
    var $li = $('#ul');
    var id = 0;
    //->bindHTML:实现数据绑定
    function bindHTML(data) {
        var str = '';
        for (var i = 0, len = data.data.list.length; i < len; i++) {
            var cur = data.data.list[i];
            str += '<li dd="'+cur.teacheruid+'">\
                      <img src="img/teacher.png" alt="">\
                      <p>' + cur.lastname + '</p>\
                      <p>' + cur.ClassInNo + '</p>\
                      <div>\
                          <p style="height:38px;"><span>简介:</span>' + cur.introduce + '</p>\
                      </div>\
                  </li>'
        }
        $content.html(str);
    }
    //绑定科目
    function bindUl(data) {
        var str = '';
        str += ' <li><a href="javascript:;" did="0" class="hover">全部</a></li>'
        for (var i = 0, len = data.data.length; i < len; i++) {
            var obj = data.data[i];
            str += ' <li><a href="javascript:;" did="'+obj.coursetypeuid+'" class="hover">' + obj.coursetypename + '</a></li>'
        }
        $('#ul').html(str);
    }
    //分科数据绑定
    function bindHTMLL(data) {
        var str = '';
        for (var i = 0, len = data.length; i < len; i++) {
            var cur = data[i];
            str += '<li dd="'+cur.id+'">\
                      <img src="img/teacher.png" alt="">\
                      <p>' + cur.name + '</p>\
                      <p>' + cur.subjectName + '</p>\
                      <div>\
                          <p style="height:38px;"><span>简介:</span>' + cur.content + '</p>\
                      </div>\
                  </li>'
        }
        $content.html(str);
        location()
    }
    // 点击跳转详情
    function location() {
        $('#content li').click(function(e){
            var li=$(e.target).closest("li");
            var id=li.attr("dd");
            window.location.href='teaching_details.html?id='+id;
        })
    }
    function li() {
        $li.children('li').find('a').first().addClass('active');
        $li.click(function (e) {
            if(e.target.tagName.toUpperCase()==='A'){
                var id=Number($(e.target).attr("did"));
                $(event.target).addClass('active').parent('li').siblings('li').children('a').removeClass('active');
                $.ajax({
                    url: 'mock/teacherlist.json',
                    dataType: 'json',
                    success: function (data) {
                        if(id===0){
                            $.ajax({
                                url: '/api/teachers?id=' + id,
                                success: function (data) {
                                    bindHTML(data);
                                    location()
                                }
                            });
                        }
                        var selectArr = [];
                        $.each(data, function (i, item) {
                            if(item.subjectId === id) {
                                selectArr.push(item)
                            }
                        });
                        bindHTMLL(selectArr);
                        /*if(id==0){
                         $.ajax({
                         url: '/api/teachers?id=' + id,
                         success: function (data) {
                         bindHTML(data);
                         }
                         });
                         }
                         for(var i=0;i<data.length;i++){
                         var cur=data[i];
                         if(cur.subjectId!=id){
                         data.splice(i,1);
                         i--;
                         }
                         }
                         bindHTMLL(data);*/
                    }
                })
            }

        })
    }
    return {
        init: function () {
            $.ajax({
                url: '/api/teachers?id=' + id,
                success: function (data) {
                    bindHTML(data);
                    location()
                }
            });
            $.ajax({
                url:'/api/coursetypes',
                success: function (data) {
                    bindUl(data);
                    li();
                }
            })
        }
    }
})();
indexRender.init();
